<?php

class Model_User extends Model_Auth_User {
    
    public function validate_create(& $array) {
        // Initialise the validation library and setup some rules
        $array = Validate::factory($array)
                    ->rules('password', $this->_rules['password'])
                    ->rules('username', $this->_rules['username'])
                    ->rules('email', $this->_rules['email'])
                    ->rules('password_confirm', $this->_rules['password_confirm'])
                    ->filter('username', 'trim')
                    ->filter('email', 'trim')
                    ->filter('password', 'trim')
                    ->filter('password_confirm', 'trim');

        #Executes username callbacks defined in parent
        foreach($this->_callbacks['username'] as $callback){
                $array->callback('username', array($this, $callback));
        }

        #Executes email callbacks defined in parent
        foreach($this->_callbacks['email'] as $callback){
                $array->callback('email', array($this, $callback));
        }

        return $array;
    }
    
    protected static $user_list;
    private static $_roles_list;
    
    
//    public static function getCurrent(){
//        $auth = Auth::instance();
//        $user = $auth->logged_in();
//        
//        
//        
//        return $user;
//    }
    
    public static function getUsers(){
        if(self::$user_list){
        }else{
            $user = new Model_User();
            self::$user_list = $user->getFullList();
        }
        return self::$user_list;
    }


    public function hasRoles($roles=array()) {
        /*
         * roles:
         * 1 - login
         * 2 - admin
         * 3 - manager
         */
        
        $allRoles = $this->getAllRoles();
        
        if(!is_array($allRoles) || !count($allRoles)){
            throw new Exception('User::hasRoles() unable to read rules');
        }
        
        foreach ($allRoles as $k=>&$v) {
            $allRoles2[$v['name']] = $v; 
        }
        unset($v); $allRoles = $allRoles2;
        
        if(is_string($roles)){
            $roles=array($roles);
        }
        
        if(is_array($roles)){
            foreach($roles as $v){
                $roles2[] = $allRoles[$v]['id'];
            }
            $roles = $roles2;
        }else{
            throw new Exception('User::hasRoles() unable to read rules');
        }

        return $this->has('roles', $roles);
    }

    public function getFullList() {   
        $r = $this->find_all();
        $res = array();
        foreach ($r as $v) {
            $t = $v->as_array();
            $res[$t['id']] = $t;
        }
        return $res;
    }

    public function getAllRoles(){
        if (!self::$_roles_list) {
            $r = Model::factory('role')->find_all();

            $res=array();
            foreach($r as $v){
                $res[]=$v->as_array();
            }
            self::$_roles_list = $res;
        }

        return self::$_roles_list;
    }


    public function clearRoles(){
        $id = $this->id;

        $q = "delete from roles_users where user_id = $id";
        $r = DB::query(Database::DELETE, $q)->execute();

        return $r;
    }
    
    
    public function addRoles($rolesIds = array(), $clearF = false){
        if($clearF){
            $this->clearRoles();
        }
        
        $user_id = $this->id;
        
        $vals = array();
        foreach($rolesIds as $role_id){
            $vals[] = "($user_id, $role_id)";
        }
        $vals = implode(',', $vals);
        
        $q = "insert into roles_users(user_id, role_id) values $vals";
        $r = DB::query(Database::INSERT, $q)->execute();
        
        
        return $r;
    }


    public function getRoles(){
        $id = $this->id;

        $res = array();
        $q = "select * from roles_users where user_id = $id";
        $r = DB::query(Database::SELECT, $q)->execute();
//        $x = $r->as_array();


        $res=array();
        foreach($r as $x){
            $res[$x['role_id']]=$x;
        }

        return $res;
    }
    
}